package office.action.hr;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import office.action.hr.vo.BroadcastVo;
import office.bo.hr.impl.Broadcast;
import office.service.hr.IBroadcastService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import platform.common.action.BaseAction;
import platform.util.DateUtil;

import current.security.user.domain.User;

@Namespace("/broadcast")
@ParentPackage("struts-base")
@Controller("broadcastAction")
@Scope("prototype")
public class BroadcastAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private List<BroadcastVo> broadcasts = new ArrayList<BroadcastVo>();
	
	private BroadcastVo broadcastVo;
	
	private IBroadcastService broadcastService;
	
	@Action(value="gotoChangeStatus",results={@Result(location="/pages/hr/broadcast/broadcastStatus.jsp")})
	public String gotoChangeStatus(){
		return "success";
	}
	
	@Action(value="gotoBroadcast",results={@Result(location="/pages/hr/broadcast/broadcastEdit.jsp")})
	public String gotoBroadcast(){
		return "success";
	}
	
	@Action(value="gotoBroadcastMenu",results={@Result(location="/pages/hr/broadcast/broadcastXml.jsp")})
	public String gotoTripMenu()throws Exception{
		getBroadcastById();
		return "success";
	}
	
	@Action(value="gotoDeleteBroadcast",results={@Result(location="/pages/hr/broadcast/broadcastDel.jsp")})
	public String gotoDeleteBroadcast(){
		return "success";
	}
	
	@Action(value="getAllBroadcast",results={@Result(location="/pages/hr/broadcast/broadcast.jsp")})
	public String getAllBroadcast(){
		List<Broadcast> bs = broadcastService.getAllBroadcast();
		if(bs != null && bs.size() >0){
			for (Iterator<Broadcast> iterator = bs.iterator(); iterator.hasNext();) {
				Broadcast broadcast = (Broadcast) iterator.next();
				BroadcastVo broadcastVo = new BroadcastVo();
				broadcastVo.setId(broadcast.getId().toString());
				broadcastVo.setName(broadcast.getName());
				broadcastVo.setContent(broadcast.getContent());
				broadcastVo.setStatus(broadcast.getStatus());
				broadcastVo.setUserId(broadcast.getUser().getId());
				broadcastVo.setCreateDate(broadcast.getCreateDate());
				broadcastVo.setUserName(broadcast.getUser().getCname());
				broadcastVo.setLeavel(broadcast.getLeavel());
				broadcasts.add(broadcastVo);
			}
		}
		return "success";
	}
	
	@Action(value="getShowIndexBroadcast")
	public String getShowIndexBroadcast(){
		List<Broadcast> bs = broadcastService.getShowIndexBroadcast("1");
		if(bs != null && bs.size() >0){
			for (Iterator<Broadcast> iterator = bs.iterator(); iterator.hasNext();) {
				Broadcast broadcast = (Broadcast) iterator.next();
				BroadcastVo broadcastVo = new BroadcastVo();
				broadcastVo.setId(broadcast.getId().toString());
				broadcastVo.setName(broadcast.getName());
				broadcastVo.setContent(broadcast.getContent());
				broadcastVo.setStatus(broadcast.getStatus());
				broadcastVo.setUserId(broadcast.getUser().getId());
				broadcastVo.setCreateDate(broadcast.getCreateDate());
				if(DateUtil.timePoor(broadcast.getCreateDate(), DateUtil.getDate(),"yyyy-MM-dd HH:mm:ss")<=1){
					broadcastVo.setIsNew("1");
				}
				broadcastVo.setLeavel(broadcast.getLeavel());
				broadcastVo.setUserName(broadcast.getUser().getCname());
				broadcasts.add(broadcastVo);
			}
		}
		return "success";
	}
	
	@Action(value="saveBroadcast")
	public String saveBroadcast(){
		if(broadcastVo != null){
			Broadcast broadcast = new Broadcast();
			if(broadcastVo.getId() != null && broadcastVo.getId().equals("")){
				broadcastVo.setId(null);
			}
			broadcast.setId(broadcastVo.getId());
			broadcast.setName(broadcastVo.getName());
			broadcast.setStatus(broadcastVo.getStatus());
			broadcast.setContent(broadcastVo.getContent());
			User user = new User();
			user.setId(getUserId());
			broadcast.setUser(user);
			broadcast.setLeavel(broadcastVo.getLeavel());
//			broadcast.setFlag(broadcastVo.getFlag());
			broadcast.setCreateDate(broadcastVo.getCreateDate());
			broadcastService.saveBroadcast(broadcast);
		}
		this.getAjaxMessagesJson().setMessage(true, "", "保存成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="getBroadcastById",results={@Result(location="/pages/hr/broadcast/broadcastEdit.jsp")})
	public String getBroadcastById(){
		Broadcast broadcast = broadcastService.getBroadcastById(broadcastVo.getId());
		broadcastVo = new BroadcastVo();
		broadcastVo.setId(broadcast.getId().toString());
		broadcastVo.setName(broadcast.getName());
		broadcastVo.setContent(broadcast.getContent());
		broadcastVo.setUserId(broadcast.getUser().getId());
		broadcastVo.setStatus(broadcast.getStatus());
		return "success";
	}
	@Action(value="deleteBroadcastById")
	public String deleteBroadcastById(){
		broadcastService.deleteBroadcastById(broadcastVo.getId());
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="changeStatusById")
	public String changeStatusById(){
		Broadcast broadcast = broadcastService.getBroadcastById(broadcastVo.getId());
		broadcast.setStatus(broadcastVo.getStatus());
		this.getAjaxMessagesJson().setMessage(true, "", "更改成功");
		return RESULT_AJAXJSON;
	}

	public List<BroadcastVo> getBroadcasts() {
		return broadcasts;
	}

	public void setBroadcasts(List<BroadcastVo> broadcasts) {
		this.broadcasts = broadcasts;
	}

	public BroadcastVo getBroadcastVo() {
		return broadcastVo;
	}

	public void setBroadcastVo(BroadcastVo broadcastVo) {
		this.broadcastVo = broadcastVo;
	}

	public IBroadcastService getBroadcastService() {
		return broadcastService;
	}

	public void setBroadcastService(IBroadcastService broadcastService) {
		this.broadcastService = broadcastService;
	}

}
